﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class JoinPrivateForum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            //this.Title = GND.ReturnAppTitle();
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            string ForumId = null;
            if (Request.QueryString["ForumId"] != null)
                ForumId = Request.QueryString["ForumId"].ToString();
            else
                Response.Redirect("~/ForumHomePage.aspx");
            
            ForumIdHiddenField.Value = ForumId;

            if ((GND.ReadNivdalBoolData("@ForumId", "Nivdal_ForumIsForumPublic", ForumId)) ||
                (GND.ReadNivdalBoolData("@UserId", "@ForumId", "Nivdal_ForumIsUserForumMember", ou.UserId, ForumId)))
                Response.Redirect("~/ForumDetails.aspx?Id=" + ForumId);
            if (Request.QueryString["Answer"] != null)
                this.MessageLabel.Text = Request.QueryString["Answer"].ToString();

            this.ForumNameLabel.Text = GND.ReadNivdalStringData("@ForumId", "Nivdal_ForumGetForumName", ForumId);
            ForumNameLabel2.Text = this.ForumNameLabel.Text;
            this.ForumLinkButon.Text = this.ForumNameLabel.Text;

            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.JoinPrivateForum_PageTitle + ": " + this.ForumNameLabel.Text;
            //this.Title = this.ForumNameLabel.Text + " " + " - nivdal.com!";
            if (GND.ReadNivdalBoolData("@ForumId", "Nivdal_IsForumDeleted", ForumId))
            {
                this.ForumInfoLabel.Text = Resources.Resource.JoinPrivateForum_DeletedLabelText;
                
                this.PageNameLabel.Text = Resources.Resource.JoinPrivateForum_PageNameLabe;
                this.PageNameLabel2.Text = Resources.Resource.JoinPrivateForum_PageNameLabe;
                this.JoinForumJustifyTitleLabel.Visible = false;
                this.ApplicationNote.Visible = false;
                this.SubmitApplicationButton.Visible = false;
                this.NumForumUsersTitleLabel.Visible = false;
            }
            else
            {
                this.ForumInfoLabel.Text = GND.ReadNivdalStringData("@ForumId", "Nivdal_ForumGetForumPublicInfo", ForumId);
                this.NumForumUsersLabel.Text = GND.ReadNivdalIntData("@ForumId", "Nivdal_GetNumOfForumUsers", ForumId);
            }
        }
        protected void SubmitApplicationButton_Click(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string ForumId = ForumIdHiddenField.Value;

            String ApplicationNote = this.ApplicationNote.Text.Trim();

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_ForumUserJoinForum";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_UserId = new SqlParameter();
            dbParam_UserId.ParameterName = "@UserId";
            dbParam_UserId.Value = ou.UserId;
            dbParam_UserId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_UserId);

            SqlParameter dbParam_ForumId = new SqlParameter();
            dbParam_ForumId.ParameterName = "@ForumId";
            dbParam_ForumId.Value = ForumId;
            dbParam_ForumId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ForumId);

            SqlParameter dbParam_ApplicationNote = new SqlParameter();
            dbParam_ApplicationNote.ParameterName = "@ApplicationNote";
            dbParam_ApplicationNote.Value = ApplicationNote;
            dbParam_ApplicationNote.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ApplicationNote);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                if (dbParam_ForumId.Value.ToString() != "0")
                    Response.Redirect("JoinPrivateForum.aspx?ForumId=" + dbParam_ForumId.Value.ToString() + "&Answer=" + dbParam_Answer.Value.ToString());
                else
                    this.MessageLabel.Text = dbParam_Answer.Value.ToString();
            }
            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}